using System;
using System.Collections;
using System.Collections.Generic;
using SUIFW;
using UnityEngine;

/*
 主题：UI窗体的父类
    功能：
    定义所有UI窗体的父类
    定义四个生命周期
    
    1；显示状态
    2：Hiding隐藏状态
    3：ReDisplay 再显示状态
    4：Freeze 冻结状态
*/
public class BaseUIForm : MonoBehaviour
{
    //字段
    private UIType _currentUIType = new UIType();

    //属性
    //当前UI窗体类型
    public UIType CurrentUIType
    {
        get => _currentUIType;
        set => _currentUIType = value;
    }

    #region 窗体四种状态
    /// <summary>
    /// 显示状态
    /// </summary>
    public virtual void Display()
    {
        gameObject.SetActive(true);
        //必须是弹出窗体 设置其模态窗体调用
        if (CurrentUIType.UIForms_Type == UIFormType.PopUp)
        {
            UIMaskMgr.Instance().SetMaskWindow(this.gameObject,CurrentUIType.UIForms_LucencyType);
        }
    }

    /// <summary>
    /// 隐藏状态
    /// </summary>
    public virtual void Hiding()
    {
        gameObject.SetActive(false);
        //取消模态窗体调用
        if (CurrentUIType.UIForms_Type == UIFormType.PopUp)
        {
            UIMaskMgr.Instance().CancelMaskWindow();
        }
    }

    /// <summary>
    /// 再显示状态
    /// </summary>
    public virtual void ReDisplay()
    {
        gameObject.SetActive(true);
    }

    /// <summary>
    /// 冻结状态
    /// </summary>
    public virtual void Freeze()
    {
        gameObject.SetActive(true);
    }
    
    #endregion
    
    #region 封装子类常用的方法

    /// <summary>
    /// 注册按钮事件
    /// </summary>
    /// <param name="buttonName">按钮名称</param>
    /// <param name="delHandler">需要注册的方法</param>
    protected void RegisterButtonObjectEvent(string buttonName, EventTriggerListener.VoidDelegate delHandler)
    {
        GameObject tra  = UnityHelper.FindTheChildNode(this.gameObject,buttonName).gameObject;
      
        //给按钮注册事件方法
        if (tra!=null)
        {
            EventTriggerListener.Get(tra).onClick = delHandler;
        }
    }


    protected void OpenUIForm(string uiName)
    {
        UIManager.Instance.ShowUIForms(uiName);
    }

    protected void CloseUIForm()
    {
        string strUIFormName = String.Empty;
        int intPosition = -1;
        strUIFormName = GetType().ToString();
        strUIFormName.IndexOf(',');
        if (intPosition!=-1)
        {
            strUIFormName = strUIFormName.Substring(intPosition+1);
        }
        
        UIManager.Instance.CloseUIForms(strUIFormName);
    }

    protected void CloseUIForm(string uiName)
    {
        UIManager.Instance.CloseUIForms(uiName);
    }
    
    protected void LogonSys(GameObject go)
    {
        Debug.Log(go.name);
    }
    #endregion
    
}
